package com.hxzy.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.hxzy.common.controller.BaseController;
import com.hxzy.dto.DemoData;
import com.hxzy.dto.Edus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Api(tags = "excel测试API")
@RestController
@RequestMapping(value = "/easy")
public class EasyExcelDemoController extends BaseController {


    private List<DemoData> data() {
        List<DemoData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            data.setStatus(  i%3==0?0:1);
            list.add(data);
        }
        return list;
    }

    @ApiOperation(value = "输出excel案例1")
    @GetMapping(value = "/out01")
    public void outputExcel01(HttpServletResponse response) throws IOException {
        //设定输出到浏览器的格式   .xlsx 对应的 mime-type类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        // 文件名
       String fileName = System.currentTimeMillis() + ".xlsx";
       //设定给浏览器，告诉响应给浏览器文件名
        response.setHeader("Content-disposition", "attachment;filename="+fileName);

       EasyExcel.write(response.getOutputStream(), DemoData.class).sheet("模板").doWrite(data());

    }

    @ApiOperation(value = "填充模板输出结果")
    @GetMapping(value = "/out02")
    public void outputExcel02(HttpServletResponse response) throws IOException {
        //设定输出到浏览器的格式   .xlsx 对应的 mime-type类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        // 文件名
        String fileName = System.currentTimeMillis() +"-resume"+ ".xlsx";
        //设定给浏览器，告诉响应给浏览器文件名
        response.setHeader("Content-disposition", "attachment;filename="+fileName);

        //读取模板
        InputStream excelInput = EasyExcelDemoController.class.getResourceAsStream("/excel/resume.xlsx");

        //读取excel模板
        // 方案1
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(excelInput).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();

            List<Edus>  eduList=new ArrayList<>();
            eduList.add(new Edus("大学"));
            eduList.add(new Edus("中学"));
            eduList.add(new Edus("小学"));

            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(eduList, fillConfig, writeSheet);

            //写入数据
            Map<String, Object> map = MapUtils.newHashMap();
            map.put("dept", "开发部");
            map.put("name", "小张");
            map.put("join", new Date());
            map.put("sex", "女");
            map.put("jg", "重庆");
            map.put("birthday", new Date());
            excelWriter.fill(map, writeSheet);
        }
    }

}
